<!DOCTYPE html><html lang="en"><head><title>BillingAgreementWithCreditCard.aspx</title></head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><meta name="groc-relative-root" content=""><meta name="groc-document-path" content="BillingAgreementWithCreditCard.aspx"><meta name="groc-project-path" content="BillingAgreementWithCreditCard.aspx.cs"><link rel="stylesheet" type="text/css" media="all" href="assets/style.css"><script type="text/javascript" src="assets/behavior.js"></script><body><div id="meta"><div class="file-path">BillingAgreementWithCreditCard.aspx.cs</div></div><div id="document"><div class="segment"><div class="code"><div class="wrapper">﻿<span class="hljs-keyword">using</span> PayPal.Api;
<span class="hljs-keyword">using</span> PayPal.Sample.Utilities;
<span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> System.Collections.Generic;
<span class="hljs-keyword">using</span> System.Web;

namespace PayPal.Sample
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">partial</span> <span class="hljs-keyword">class</span> BillingAgreementWithCreditCard : BaseSamplePage
    {
        <span class="hljs-keyword">protected</span> <span class="hljs-keyword">override</span> <span class="hljs-keyword">void</span> <span class="hljs-title">RunSample</span>()
        {</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="api-context">Api Context</h3>
<p>Pass in a <code>APIContext</code> object to authenticate 
the call and to send a unique request id 
(that ensures idempotency). The SDK generates
a request id if you do not pass one explicitly. 
See <a href="/Source/Configuration.html">Configuration.cs</a> to know more about APIContext.</p></div></div><div class="code"><div class="wrapper">            <span class="hljs-keyword">var</span> apiContext = Configuration.GetAPIContext();</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>For demonstration purposes, we&#39;ll first setup a billing plan in
an active state to be used to make an agreement with.</p></div></div><div class="code"><div class="wrapper">            <span class="hljs-keyword">var</span> plan = BillingPlanCreate.CreatePlanObject(HttpContext.Current);
            <span class="hljs-keyword">var</span> guid = Convert.ToString((<span class="hljs-keyword">new</span> Random()).Next(<span class="hljs-number">100000</span>));
            plan.merchant_preferences.return_url = Request.Url.ToString() + <span class="hljs-string">"?guid="</span> + guid;
            plan.merchant_preferences.cancel_url = Request.Url.ToString();</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><blockquote>
<p>Note: Both <code>return_url</code> and <code>cancel_url</code> are being set in
<code>plan.merchant_preferences</code> since many plans may be setup well
in advance of anyone setting up an agreement for it.  These URLs
are only used for agreements setup with <code>payer.payment_method</code>
set to <code>paypal</code> to establish where the customer should be redirected
once they approve or cancel the agreement using their PayPal account.</p>
</blockquote></div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">            // Ignore workflow code segment</span></div><div class="wrapper">            <span class="hljs-comment">// Ignore workflow code segment</span>
            <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
            <span class="hljs-keyword">this</span>.flow.AddNewRequest(<span class="hljs-string">"Create new billing plan"</span>, plan);
            <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span>

            <span class="hljs-keyword">var</span> createdPlan = plan.Create(apiContext);</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">            // Ignore workflow code segment</span></div><div class="wrapper">            <span class="hljs-comment">// Ignore workflow code segment</span>
            <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
            <span class="hljs-keyword">this</span>.flow.RecordResponse(createdPlan);
            <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Activate the plan</p></div></div><div class="code"><div class="wrapper">            <span class="hljs-keyword">var</span> patchRequest = <span class="hljs-keyword">new</span> PatchRequest()
            {
                <span class="hljs-keyword">new</span> Patch()
                {
                    op = <span class="hljs-string">"replace"</span>,
                    path = <span class="hljs-string">"/"</span>,
                    <span class="hljs-keyword">value</span> = <span class="hljs-keyword">new</span> Plan() { state = <span class="hljs-string">"ACTIVE"</span> }
                }
            };</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">            // Ignore workflow code segment</span></div><div class="wrapper">            <span class="hljs-comment">// Ignore workflow code segment</span>
            <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
            <span class="hljs-keyword">this</span>.flow.AddNewRequest(<span class="hljs-string">"Update the plan"</span>, patchRequest);
            <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span>

            createdPlan.Update(apiContext, patchRequest);</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">            // Ignore workflow code segment</span></div><div class="wrapper">            <span class="hljs-comment">// Ignore workflow code segment</span>
            <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
            <span class="hljs-keyword">this</span>.flow.RecordActionSuccess(<span class="hljs-string">"Plan updated successfully"</span>);
            <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>With the plan created and activated, we can now create the billing agreement.
A resource representing a Payer that funds a payment.</p></div></div><div class="code"><div class="wrapper">            <span class="hljs-keyword">var</span> payer = <span class="hljs-keyword">new</span> Payer
            {
                payment_method = <span class="hljs-string">"credit_card"</span>,
                funding_instruments = <span class="hljs-keyword">new</span> List&lt;FundingInstrument&gt;
                {
                    <span class="hljs-keyword">new</span> FundingInstrument
                    {
                        credit_card = <span class="hljs-keyword">new</span> CreditCard
                        {
                            billing_address = <span class="hljs-keyword">new</span> Address
                            {
                                city = <span class="hljs-string">"Johnstown"</span>,
                                country_code = <span class="hljs-string">"US"</span>,
                                line1 = <span class="hljs-string">"52 N Main ST"</span>,
                                postal_code = <span class="hljs-string">"43210"</span>,
                                state = <span class="hljs-string">"OH"</span>
                            },
                            cvv2 = <span class="hljs-string">"874"</span>,
                            expire_month = <span class="hljs-number">11</span>,
                            expire_year = <span class="hljs-number">2018</span>,
                            first_name = <span class="hljs-string">"Joe"</span>,
                            last_name = <span class="hljs-string">"Shopper"</span>,
                            number = <span class="hljs-string">"4877274905927862"</span>,
                            type = <span class="hljs-string">"visa"</span>
                        }
                    }
                }
            };

            <span class="hljs-keyword">var</span> shippingAddress = <span class="hljs-keyword">new</span> ShippingAddress()
            {
                line1 = <span class="hljs-string">"111 First Street"</span>,
                city = <span class="hljs-string">"Saratoga"</span>,
                state = <span class="hljs-string">"CA"</span>,
                postal_code = <span class="hljs-string">"95070"</span>,
                country_code = <span class="hljs-string">"US"</span>
            };

            <span class="hljs-keyword">var</span> agreement = <span class="hljs-keyword">new</span> Agreement()
            {
                name = <span class="hljs-string">"T-Shirt of the Month Club"</span>,
                description = <span class="hljs-string">"Agreement for T-Shirt of the Month Club"</span>,
                start_date = <span class="hljs-string">"2015-02-19T00:37:04Z"</span>,
                payer = payer,
                plan = <span class="hljs-keyword">new</span> Plan() { id = createdPlan.id },
                shipping_address = shippingAddress
            };</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">            // Ignore workflow code segment</span></div><div class="wrapper">            <span class="hljs-comment">// Ignore workflow code segment</span>
            <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
            <span class="hljs-keyword">this</span>.flow.AddNewRequest(<span class="hljs-string">"Create billing agreement"</span>, agreement);
            <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Create the billing agreement.</p></div></div><div class="code"><div class="wrapper">            <span class="hljs-keyword">var</span> createdAgreement = agreement.Create(apiContext);</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">            // Ignore workflow code segment</span></div><div class="wrapper">            <span class="hljs-comment">// Ignore workflow code segment</span>
            <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
            <span class="hljs-keyword">this</span>.flow.RecordResponse(createdAgreement);
            <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>For more information, please visit <a href="https://developer.paypal.com/docs/api/">PayPal Developer REST API Reference</a>.</p></div></div><div class="code"><div class="wrapper">        }
    }
}</div></div></div></div></body></html>